package LeetCode;

public class Code34_searchRange {

    public int[] searchRange(int[] nums, int target) {
        int a = search(nums, target);
        int b = search(nums, target + 1);
        if (a == nums.length || nums[a] != target){
            return new int[]{-1, -1};
        } else {
            return new int[]{a, b - 1};
        }
    }
    // 返回>=target的第一个数
    private int search(int[] nums, int target) {
        int l = 0;
        int r = nums.length;
        while (l < r){
            int m = l + ((r - l) >> 1);
            if (nums[m] < target){
                l = m + 1;
            } else {
                r = m;
            }
        }
        return l;
    }

    public static void main(String[] args) {
        Code34_searchRange code34_searchRange = new Code34_searchRange();
//        int[] res = code34_searchRange.searchRange(new int[]{5,7,7,8,8,10}, 8);
        int[] res = code34_searchRange.searchRange(new int[]{5,7,7,8,8,10}, 8);
        for (int i = 0; i < 2; i++) {
            System.out.println(res[i]);
        }
    }
}
